package com.zhp.farmerclient.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhp.farmerclient.common.Result;
import com.zhp.farmerclient.entity.Comment;
import com.zhp.farmerclient.mapper.CommentMapper;
import com.zhp.farmerclient.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentService {

    @Autowired
    public CommentMapper commentMapper;
    @Autowired
    public UserMapper userMapper;

    //获取评论列表
    public Result getComments(int postId){
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("post_id", postId);
        List<Comment> comment = commentMapper.selectList(wrapper);

        return Result.succ(comment,null);
    }
    //添加评论
    public Result addComments(Comment comment){
        commentMapper.insert(comment);
        return Result.succ();
    }

    //分页显示（查询）
    public Result page(Integer pageNum, Integer pageSize, String post_id, String content, String user_id) {
        Page<Comment> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        if (!"".equals(post_id)) {
            queryWrapper.like("post_id", post_id);
        }
        if (!"".equals(content)) {
            queryWrapper.like("content",content);
        }
        if (!"".equals(user_id)) {
            queryWrapper.like("user_id",user_id);
        }
        queryWrapper.orderByDesc("id");
        IPage<Comment> iPage = commentMapper.selectPage(page,queryWrapper);
        return Result.succ(iPage.getRecords(),iPage.getTotal());
    }

    //删除
    public Result delete(Integer id) {
        commentMapper.deleteById(id);
        return Result.succ();
    }

    //批量删除
    public Result deletes(List<Integer> ids) {
        commentMapper.deleteBatchIds(ids);
        return Result.succ();
    }


}
